package mathwork;

import utility.TreeEntryDouble;



public class NormalDistributionTable extends Integral{

	TreeEntryDouble ptree=null;
	TreeEntryDouble ztree=null;
	
	@Override
	protected Double f(Double x) {
		return (1/Math.sqrt(2*Math.PI))*Math.exp(-1*x*x/2);
	}
	
	public NormalDistributionTable(){
		/*Initialize ptree*/
		ptree=new TreeEntryDouble(0.50,this.GetZ(0.5));
		Double loop;
		for(loop=0.00;loop<4.99;loop+=0.01){
			TreeEntryDouble tmp=new TreeEntryDouble(this.trapz(0.0, loop)*2,loop);
			ptree.InsertAtIncreasedOrder(tmp);
		}
		
		ztree=new TreeEntryDouble(2.50,this.GetPercent(2.50));
		for(loop=0.00;loop<4.99;loop+=0.01){
			TreeEntryDouble tmp=new TreeEntryDouble(loop,this.trapz(0.0, loop)*2);
			ztree.InsertAtIncreasedOrder(tmp);
		}
		
		return;
	}
	
	public Double GetZ(Double percent){
		Double result=0.0;
		Double loop;
		for(loop=0.00;loop<4.99;loop+=0.01){
			result=this.trapz(0.0, loop);
			if(percent.compareTo(result*2.0)<=0){
				break;
			}
		}
		
		return Mathwork.Roundup(loop, 2);
	}
	
	public Double FastGetZ(Double percent){
		return Mathwork.Roundup((Double)(ptree.FindCeilingClose(percent).GetValue()), 2);
	}
	
	public Double GetPercent(Double z){
		return Mathwork.Roundup(this.trapz(0.0, z)*2,2);
	}
	
	public Double FastGetPercent(Double z){
		return Mathwork.Roundup((Double)(ztree.FindCeilingClose(z).GetValue()),2);
	}
	
	public void Output(){
		Double result=0.0;
		Double loop;
		for(loop=0.00;loop<4.99;loop+=0.01){
			result=this.trapz(0.0, loop);
			System.out.println(Mathwork.Roundup(loop, 2)+"->"+Mathwork.Roundup(result, 7));
		}
	}

}
